package com.evan.webadmin.biz.controller;


import com.evan.common.core.annotation.MyRequestBody;
import com.evan.common.core.annotation.NoAuthInterface;
import com.evan.common.core.constant.BizConstant;
import com.evan.common.core.object.MyPageData;
import com.evan.common.core.object.MyPageParam;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.object.TokenData;
import com.evan.common.core.upload.UploadResponseInfo;
import com.evan.common.core.util.MyModelUtil;
import com.evan.common.core.util.MyPageUtil;
import com.evan.common.minio.util.MinioUpDownloader;
import com.evan.webadmin.biz.dto.MaterialDto;
import com.evan.webadmin.biz.dto.MaterialFilter;
import com.evan.webadmin.biz.helper.DictHelper;
import com.evan.webadmin.biz.model.Material;
import com.evan.webadmin.biz.service.IMaterialService;
import com.evan.webadmin.biz.vo.MaterialFrontVo;
import com.evan.webadmin.biz.vo.MaterialVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Pavel
 * @since 2024-06-09
 */
@RestController
@RequestMapping("/admin/biz/material")
public class MaterialController {

    @Autowired
    IMaterialService materialService;
    @Autowired
    DictHelper dictHelper;
    @Autowired
    MinioUpDownloader minioUpDownloader;

    //题目列表 根据人员类型 题目类型 模块类型 题目名称搜素
    @PostMapping("/findByCondition")
    public ResponseResult<MyPageData<MaterialVo>> findByCondition(@MyRequestBody MaterialFilter materialFilter,
                                                                @MyRequestBody MyPageParam pageParam) {

        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<Material> materials = materialService.findByCondition(materialFilter);
        long totalCount = 0L;
        if (materials instanceof Page) {
            totalCount = ((Page<Material>) materials).getTotal();
        }
        List<MaterialVo> materialVos = MyModelUtil.copyCollectionTo(materials, MaterialVo.class);
        materialVos.forEach(i-> {
            i.setMaterial(dictHelper.getDictMap(BizConstant.DICTTYPE_MATERIAL_CODE).get(i.getMaterialType() + ""));
            i.setModule(dictHelper.getDictMap(BizConstant.DICTTYPE_MODULE_CODE).get(i.getModuleType() + ""));
            i.setScene(dictHelper.getDictMap(BizConstant.DICTTYPE_SCENE_CODE).get(i.getSceneType() + ""));
            i.setPersonnel(dictHelper.getDictMap(BizConstant.DICTTYPE_PERSONNEL_CODE).get(i.getPersonnelType() + ""));
        });
        return ResponseResult.success(MyPageUtil.makeResponseData(materialVos, totalCount));
    }

    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody MaterialDto materialDto) {
        materialService.edit(materialDto);
        return ResponseResult.success();
    }

    @PostMapping("/upload")
    public ResponseResult<UploadResponseInfo> upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws IOException {
        UploadResponseInfo responseInfo = minioUpDownloader.doUpload(req.getContextPath(), "", "material","", false, file);
        return ResponseResult.success(responseInfo);
    }

    @GetMapping("/download/{fileName}")
    @NoAuthInterface
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        minioUpDownloader.doPreView("", "material","", fileName,false, response);
    }

    @GetMapping("/findById")
    public ResponseResult<Material> findById(@RequestParam String materialId)  {
        return ResponseResult.success(materialService.findById(Long.valueOf(materialId)));
    }

    @PostMapping("/findByFront")
    public ResponseResult<List<MaterialFrontVo>> findByFront(@MyRequestBody MaterialFilter materialFilter, HttpServletRequest request){
        TokenData tokenData = (TokenData) request.getAttribute(TokenData.REQUEST_ATTRIBUTE_NAME);
        return ResponseResult.success(materialService.findByFront(materialFilter));
    }

    @PostMapping("/logic/delete")
    public ResponseResult logicDelete(@RequestBody MaterialDto materialDto) {
        materialService.logicDelete(materialDto.getMaterialId());
        return ResponseResult.success();
    }
}

